package com.hosa.ai.robot.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/ai")
public class DeepSeekChatController {
    @Resource
    private DeepSeekChatModel chatModel;

    @GetMapping("/generate")
    public String generate(@RequestParam(value = "message",defaultValue = "你是谁？") String message) {
        return chatModel.call( message);
    }

    @GetMapping(value = "/generateStream", produces = "text/html;charset=UTF-8")
    public Flux<String> generateStream(@RequestParam(value = "message",defaultValue = "你是谁？") String message) {
        //构建提示词
        Prompt prompt = new Prompt(new UserMessage(message));

        //流式输出
        return chatModel.stream(prompt)
                .mapNotNull(chatResponse ->
                        chatResponse.getResult().getOutput().getText());
    }
}
